<?php

class NotificaAdminBehavior extends ModelBehavior {


	public function setup(Model $Model, $config = array()) {
		// print_r($Model);
	}

/**
 * Cleanup Callback unbinds bound translations and deletes setting information.
 *
 * @param Model $Model Model being detached.
 * @return void
 */
	// public function cleanup(Model $Model) {
	// }

/**
 * beforeFind Callback
 *
 * @param Model $Model Model find is being run on.
 * @param array $query Array of Query parameters.
 * @return array Modified query
 */
	// public function beforeFind(Model $Model, $query) {
		// return $query;
	// }




/**
 * afterFind Callback
 *
 * @param Model $Model Model find was run on
 * @param array $results Array of model results.
 * @param boolean $primary Did the find originate on $model.
 * @return array Modified results
 */
	// public function afterFind(Model $Model, $results, $primary) {
		// return $results;
	// }

/**
 * beforeValidate Callback
 *
 * @param Model $Model Model invalidFields was called on.
 * @return boolean
 */
	// public function beforeValidate(Model $Model) {
// 
		// return true;
	// }

/**
 * beforeSave callback.
 *
 * @param Model $Model Model save was called on.
 * @return boolean true.
 */
	// public function beforeSave(Model $Model, $options = array()) {
		// CakeLog::debug(json_encode($Model));
		// return true;
	// }


/**
 * afterSave Callback
 *
 * @param Model $Model Model the callback is called on
 * @param boolean $created Whether or not the save created a record.
 * @return void
 */
	public function afterSave(Model $Model, $created) {
		try {
			$this->loadModel("Notifica");
			if ($created) {
				if ($Model->alias=="User") {
					foreach ($this->getAdministrators() as $key => $administrator) {
						$this->Notifica->set("user_id",$administrator["User"]["id"]);
						$this->Notifica->set("oggetto",$Model->alias);
						$this->Notifica->set("oggetto_id",$Model->data[$Model->alias]["id"]);
						$this->Notifica->set("oggetto_stato",$Model->data[$Model->alias]["stato"]);
						$this->Notifica->set("stato",NOTIFICA_NUOVA);
						$this->Notifica->set("tipo",NOTIFICA_TIPO_VISIONE);
						$this->Notifica->set("url", Router::url( '/Users/profilo/',false).$Model->data[$Model->alias]["username"]);
						$this->Notifica->save();
					}
				}
			}
			return true;
		} catch (exception $e) {
			CakeLog::error($e->getMessage());
			throw $e;
			return false;
		}
	}

	private function loadModel($modelName)  {
	    App::import('Model',$modelName); //load model..
	    $this->{$modelName} = new $modelName(); //and create an instance of it
	}

	private function newModel($modelName)  {
	    $this->{$modelName} = new $modelName(); //and create an instance of it
	}


/**
 * afterDelete Callback
 *
 * @param Model $Model Model the callback was run on.
 * @return void
 */
	// public function afterDelete(Model $Model) {
// 
	// }

	private function getAdministrators() {
		$administrators = Cache::read("administrators","short");
		if (empty($administrators) || count($administrators)==0) {
			$this->loadModel("User");
			$administrators = $this->User->find("all",array("conditions"=>array("User.stato"=>USER_ATTIVO, "User.group_id"=>CakeSession::read("parametri.".PARAMETRO_GRUPPO_ADMINISTRATORS)), "recursive"=>-1));
			Cache::write("administrators", $administrators, "short");
		}
		return $administrators;
		
	}

}
